$(function() {
	setWXConfig(document.location.href);

	//setWXConfig(preUrl+"wechat/user/inquire.html");
	// 用户编号选择
	var user_num = $('.header .user_num').text();
	var serial00 = $('.serial-ch .building span').text();
	$('.header a').bind('click', function(event) {
		
		getUserHome(function(){
			$('.main').hide();
			$('.serial-main').show();
		});
	});
	
	$('.serial-main .serial .serial-ch').bind(
			'click',
			function(event) {
				var serial = $(this).find('.building span').text();
				
				$(this).find('.state').text('已选择').parent().siblings(
						'.serial-ch').find('.state').text('');
				$('.header .user_num').text(serial);
				$('.serial-main').hide();
				$('.main').show();
			});

	// 点击确定按钮时，使用其他用户编号
	$('.add .btn-input').bind('click', function(event) {

		var  userNo = $(this).siblings('.write').val();
		if (userNo == '') {
			alert('用户编号不能为空，请重新输入用户编号！');
			return ;
		} else {
			var exists=false;
			$("#hoseList .building span").each(function(){
				var existUserNo=$(this).text();
				if(userNo==existUserNo){
					exists=true;
					return false;
				}
			});
			if(exists){
				alert("已存在该用户编号，请直接选择");
				return ;
			}
			
			$.ajax({
				url : preUrl+"home/queryExistUser",
				type : "POST",
				dataType : "json",
				data : {"userNo" : userNo},
				success : function(data) {
					if(data.rspCode=="0000"){
						var  homeInfo = data.data;
						$(".title p.user_num").text(homeInfo.hsUserNo);
						$("#userHomeName").text(homeInfo.hsUserName);
						$("#userTel").text(homeInfo.hsPhoneNo);
						$('.serial-main .serial .serial-ch .state').text('');
						//$('.header .user_num').text(input_content);
						$('.serial-main').hide();
						$('.main').show();
					}else{
						alert(data.rspMsg||"系统繁忙，请稍后重试");
					}
					
				}
			});
			
		}
	});
});
//根据对应的用户编号 的显示 相对应的个人信息
$(document).ready(function() {
	var urlinfo = window.location.href;
	var param=getUrlParameters();
	var userId=param.userId;
	var userNoHome=param.homeUserNo;
	$.ajax({
		url : preUrl+"home/selectUserNo",
		type : "POST",
		dataType : "json",
		data : {"homeUserNo" : userNoHome,"userId":userId},
		success : function(data) {
			if (data.rspCode == "0000") {
				// 判断 当前房源 信息
				var  homeInfo = data.data;
				$(".title p.user_num").text(homeInfo.hsUserNo);
				$("#userHomeName").text(homeInfo.hsUserName);
				$("#userTel").text(homeInfo.hsPhoneNo);
			}else{
				alert(data.rspMsg||"系统繁忙，稍后重试");
				return;
			}
			
		}
	});
});
// 根据用户查询所有的房源信息  
function getUserHome(func) {
	var urlinfo = window.location.href;
	var param=getUrlParameters();
	var userId=param.userId;
	var userNoHome=param.homeUserNo;
	$("#hoseList").html("");
	$.ajax({
		url :preUrl+"common/getUserId",
		type : "POST",
		dataType : "json",
		data : {"userId" : userId},
		success : function(data) {
			if (data.data == "undefined"||data.data == null) {
				alert("服务器加载失败");
				return ;
			}
			$("#hoseList").html("");
			for ( var i = 0; i < data.data.length; i++) {
				var temp = $("#ApplyMoeny").clone().show().removeAttr("id");
				var pBuild=temp.find("p.building");
				pBuild.append($("<span></span>").text(data.data[i].HS_USER_NO));
				pBuild.attr("val",data.data[i].HS_USER_NO);
				pBuild.append(" "+data.data[i].HS_NAME);
				var selected = "";
				// 是房源的userNO 如果userNo为这个则表示已选中
				if (data.data[i].HS_USER_NO == userNoHome) {
					selected = "已选择";
				}
				if (data.data.length - 1 == i) {
					temp.addClass("serial-ch00");
				}
				temp.find("p.state").html(selected);
				$("#hoseList").append(temp);
			}
			if(typeof func=='function'){
				func();
			}

		}
	});

}
// 选中房源查询房源信息
function gethome(obj) {
	var param=getUrlParameters();
	var userId=param.userId;
	//获取房源编号 
	var userNo = $(obj).find("p.building").attr("val");
	window.location.href =preUrl+"wechat/user/inquire.html?userId="+ userId+"&homeUserNo="+userNo;

}


//确认缴费按钮
function  toPay(){
	var urlinfo = window.location.href;
	var param=getUrlParameters();
	var userId=param.userId;
	var  orderAmt=$("#payAmt").val();
	if(orderAmt==null||orderAmt==""){
		alert("请输入金额");
		return ;
	}
	if(!/^[0-9]+(.[0-9]{1,2})?$/.test(orderAmt)){
		alert("请输入正确金额格式，小数请保留两位");
		return ;
	}
	var openId=getOpenId();
	if(openId==null||openId==""){
		alert("暂时无法支付");
		return ;
	}
	if(!confirm("请确认好用户编号和姓名手机号")){
		return ;
	}
	$.ajax({
		url :preUrl+"pay/addOrder",
		type : "POST",
		dataType : "json",
		data : {"userId" :userId,"openId":openId,"orderAmt":orderAmt,
			"userNo":$(".title p.user_num").text(),
			"userName":$("#userHomeName").text(),
			"userPhone":$("#userTel").text()},
		success : function(data) {
			if(data.rspCode=="0000"){
				var tokenId=data.data.tokenId;
				if(tokenId==""||tokenId==null){
					alert("支付失败，请重新支付");
					return ;
				}
				window.location.href = 'https://pay.swiftpass.cn/pay/jspay?token_id=' + tokenId + '&showwxpaytitle=1';
			}else{
				alert(data.rspMsg||"系统繁忙，请稍后重试");
			}
			
		},
		error:function(data){
			alert("系统繁忙，请稍后重试");
		}
	});
	return ;
	
}

